<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Banner extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Banner_model');
    }

    public function index() {
        if (!$this->checkLogin()) {
            header('Location: /biz/login/');
            return false;
        }
        $banners = $this->Banner_model->getAllBanners($this->bizId);
        $this->datas['banners'] = $banners;
        $this->load->view('banner/index', $this->datas);
    }

    public function addBanner() {
        if (!$this->checkLogin()) {
            header('Location: /biz/login/');
            return false;
        }

        $uploadPath = $this->config->config['banner_upload_path'];
        $uploadPath = $uploadPath.date('Y-m-d', time()).'/';
        $this->load->library('Cusupload');
        $this->cusupload->setAllowType('jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP');
        $this->cusupload->setAllowSize('2M');
        $this->cusupload->setEncriptname(true);
        $file = $this->cusupload->doUpload($uploadPath, 'banner');
        if ($file === false) {
            outputError('400', 'banner上传失败。');
            return false;
        }
        $file = str_replace($this->config->config['banner_upload_path'], '', $file);
        $url = $this->config->config['banner_url_path'].$file;

        $this->Banner_model->addBanner($this->bizId, $url);
        header('Location: /banner/');
        return true;
    }

    /**
     * ajax
     */
    public function delBanner() {
        if (!$this->checkLogin()) {
            outputError('403', '请登录。');
            return false;
        }

        $bannerId = $this->input->post('id', true);
        $bannerId = intval($bannerId);
        if ($bannerId <= 0) {
            outputError('403', '非法ID。');
            return false;
        }

        $banner = $this->Banner_model->getBannerById($bannerId);
        if (!$banner) {
            outputError('403', '非法ID。');
            return false;
        }

        if ($this->bizId != $banner['busiId']) {
            outputError('403', '非法ID。');
            return false;
        }

        $this->Banner_model->delBanner($bannerId);

        outputJson(200, 'OK');
    }

    /**
     * ajax
     */
    public function setBannerOrder() {
        if (!$this->checkLogin()) {
            outputError('403', '请登录。');
            return false;
        }

        $bannerId = $this->input->post('id', true);
        $bannerId = intval($bannerId);
        if ($bannerId <= 0) {
            outputError('403', '非法ID。');
            return false;
        }

        $banner = $this->Banner_model->getBannerById($bannerId);
        if (!$banner) {
            outputError('403', '非法ID。');
            return false;
        }

        if ($this->bizId != $banner['busiId']) {
            outputError('403', '非法ID。');
            return false;
        }

        $order = $this->input->post('order', true);
        $order = intval($order);
        if ($order <= 0) {
            $order = 1;
        }

        $this->Banner_model->setBannerOrder($bannerId, $order);

        outputJson(200, 'OK');
    }
}
